package com.newland.esp.service.user.impl;

import com.newland.esp.common.constant.RedisKeyConstant;
import com.newland.esp.common.util.login.CookieUtil;
import com.newland.esp.service.user.CookieService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @param
 * @author lw
 * @descripe
 * @date 2021/8/14 18:04
 * @Version 1.0
 **/
@Service
public class CookieServiceImpl implements CookieService {

    private static Logger logger = LoggerFactory.getLogger(CookieServiceImpl.class);

    /**
     * 设置 Cookie
     * @param request request
     * @param response response
     * @param ticket ticket
     * @return boolean
     */
    @Override
    public boolean setCookie(HttpServletRequest request, HttpServletResponse response,
                             String ticket) {
        try {
            CookieUtil.setCookie(request, response, RedisKeyConstant.USER_TICKET, ticket);
            return true;
        } catch (Exception e) {
            logger.error("Cookie 设置失败！ ");
        }
        return false;
    }

    /**
     *  获取 cookie
     * @param request
     * @return
     */
    @Override
    public String getCookie(HttpServletRequest request) {

        return CookieUtil.getCookieValue(request, RedisKeyConstant.USER_TICKET);
    }

    /**
     *  删除 cookie
     * @param request
     * @param response
     * @return
     */
    @Override
    public boolean deleteCookie(HttpServletRequest request, HttpServletResponse response) {
        try {
            CookieUtil.deleteCookie(request, response, RedisKeyConstant.USER_TICKET);
            return true;
        } catch (Exception e) {
            logger.error("Cookie 删除失败！ ");
        }
        return false;
    }
}
